package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;


@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 查询今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO businessData() {

        LocalDate today = LocalDate.now();
        LocalDateTime beginTime =LocalDateTime.of(today, LocalTime.MIN);//最小时间
        LocalDateTime endTime = LocalDateTime.of(today,LocalTime.MAX);//最大时间

        //新增用户
       Integer newUsers = userMapper.getByTodayCount(beginTime,endTime);
        newUsers = newUsers == null ? 0 : newUsers;
       log.info("新增用户：{}",newUsers);

        Map map = new HashMap();
        map.put("begin",beginTime);
        map.put("end",endTime);
        Integer totalOrderCount = Integer.valueOf(orderMapper.getOrderCount(map));
        log.info("订单完成率，{}",totalOrderCount);
        map.put("status", Orders.COMPLETED);//已经完成的订单数量
        //有效单数
        Integer validOrderCount = Integer.valueOf(orderMapper.getOrderCount(map));;
        //订单完成率
        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0){
            orderCompletionRate = validOrderCount.doubleValue() / totalOrderCount.doubleValue();
        }
        log.info("有效订单数，{}",orderCompletionRate);
        //营业额
        Double turnover = orderMapper.sumByMap(map);
        turnover = turnover == null ? 0.0 : turnover;
        log.info("营业额，{}",turnover);
        //平均客单价
        Double unitPrice = 0.0;
        if(validOrderCount != 0) {
            unitPrice = turnover / validOrderCount;
            unitPrice = unitPrice == null ? 0.0 : unitPrice;
        }
        log.info("平均客单价，{}",unitPrice);
        return BusinessDataVO
                .builder()
                .newUsers(newUsers)
                .unitPrice(unitPrice)
                .orderCompletionRate(orderCompletionRate)
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .build();
    }

    /**
     * 查询套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {

        Integer sold = setmealMapper.getSetmeaStatus(StatusConstant.ENABLE);
        log.info("开启的数量为：{}",sold);
        Integer discontinued = setmealMapper.getSetmeaStatus(StatusConstant.DISABLE);
        log.info("关闭的数量为：{}",discontinued);
        return SetmealOverViewVO
                .builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询菜品总览
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {

        Integer sold = dishMapper.getDishStatus(StatusConstant.ENABLE);
        log.info("开启的数量为：{}",sold);
        Integer discontinued = dishMapper.getDishStatus(StatusConstant.DISABLE);
        log.info("关闭的数量为：{}",discontinued);
        return DishOverViewVO
                .builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     *
     * @return
     */
    @Override
    public OrderOverViewVO overviewOrders() {
        //待接单数量
        Integer waitingOrders = orderMapper.getStatistics(Orders.TO_BE_CONFIRMED);
        //待派送数量
        Integer deliveredOrders = orderMapper.getStatistics(Orders.CONFIRMED);
        //已完成数量
        Integer completedOrders = orderMapper.getStatistics(Orders.COMPLETED);
        //已取消数量
        Integer cancelledOrders = orderMapper.getStatistics(Orders.CANCELLED);
        //全部订单
        Integer allOrders = orderMapper.getStatistics(Orders.ALL_ORDERS);

        return OrderOverViewVO
                .builder()
                .allOrders(allOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
}
